﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Example1.Tests
{
    [TestClass]
    public class RouteTests2
    {
        private static RouteCollection _routes;

        [ClassInitialize]
        public static void Setup(TestContext testContext)
        {
            _routes = new RouteCollection();

            _routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //with default values
            _routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

            // static URL segments
            _routes.MapRoute(
              name: "requests",
              url: "requests/show/{id}"
          
              );
        }


        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void Test_failed_url()
        {
            RouteTestHelper.TestRouteFail(_routes, "");

         
        }
        [TestMethod]
        public void Test_decompose_url()
        {

            RouteTestHelper.TestRouteMatch(_routes, "~/", "Home", "index");
            RouteTestHelper.TestRouteMatch(_routes, "~/Admin/index", "Admin", "index");
            RouteTestHelper.TestRouteMatch(_routes, "~/Customer/Add", "Customer", "add");
            RouteTestHelper.TestRouteMatch(_routes, "~/Admin", "Admin", "index");
            RouteTestHelper.TestRouteMatch(_routes, "~/Customer/Edit/1" , "Customer" , "Edit" , new {id = "1"} );
            RouteTestHelper.TestRouteMatch(_routes, "~/requests/delete/1", "requests", "delete", new { id = "1" });

        }


        [TestMethod]
        public void Test_generate_url()
        {
            RouteTestHelper.TestGenerateUrl(_routes, "Customer", "Add", "/Customer/Add");
            RouteTestHelper.TestGenerateUrl(_routes, "Admin", "Add", "/Admin/Add");
            RouteTestHelper.TestGenerateUrl(_routes, "Admin", "Index", "/Admin");
            RouteTestHelper.TestGenerateUrl(_routes, "Home", "Index", "/");
            var routeValueDictionary = new RouteValueDictionary();
            routeValueDictionary.Add("Id", "1");
            RouteTestHelper.TestGenerateUrl(_routes, "Admin", "Edit", "/Admin/Edit/1", routeValueDictionary);
            routeValueDictionary.Clear();
            routeValueDictionary.Add("key", "1");
            RouteTestHelper.TestGenerateUrl(_routes, "Admin", "Edit", "/Admin/Edit?key=1", routeValueDictionary);
            routeValueDictionary.Clear();
            routeValueDictionary.Add("Id", "1");
            RouteTestHelper.TestGenerateUrl(_routes, "Requests", "Show", "/Requests/Show/1", routeValueDictionary);
           
        }
    }
}